Convolutional Neural Networks

Machine learning on images


In [1]:
import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt

MNIST


In [2]:
from keras.datasets import mnist


Using TensorFlow backend.

In [3]:
(X_train, y_train), (X_test, y_test) = mnist.load_data('/tmp/mnist.npz')


Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz

In [4]:
X_train.shape


Out[4]:
(60000, 28, 28)

In [5]:
X_test.shape


Out[5]:
(10000, 28, 28)

In [6]:
X_train[0]


Out[6]:
array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   3,
         18,  18,  18, 126, 136, 175,  26, 166, 255, 247, 127,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,  30,  36,  94, 154, 170,
        253, 253, 253, 253, 253, 225, 172, 253, 242, 195,  64,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,  49, 238, 253, 253, 253, 253,
        253, 253, 253, 253, 251,  93,  82,  82,  56,  39,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,  18, 219, 253, 253, 253, 253,
        253, 198, 182, 247, 241,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,  80, 156, 107, 253, 253,
        205,  11,   0,  43, 154,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,  14,   1, 154, 253,
         90,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 139, 253,
        190,   2,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  11, 190,
        253,  70,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  35,
        241, 225, 160, 108,   1,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         81, 240, 253, 253, 119,  25,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,  45, 186, 253, 253, 150,  27,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0,  16,  93, 252, 253, 187,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0,   0,   0, 249, 253, 249,  64,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,  46, 130, 183, 253, 253, 207,   2,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  39,
        148, 229, 253, 253, 253, 250, 182,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  24, 114, 221,
        253, 253, 253, 253, 201,  78,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,  23,  66, 213, 253, 253,
        253, 253, 198,  81,   2,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,  18, 171, 219, 253, 253, 253, 253,
        195,  80,   9,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,  55, 172, 226, 253, 253, 253, 253, 244, 133,
         11,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0, 136, 253, 253, 253, 212, 135, 132,  16,   0,
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0,   0]], dtype=uint8)

In [7]:
plt.imshow(X_train[0], 
           cmap = 'gray')


Out[7]:
<matplotlib.image.AxesImage at 0x7ff48d3035c0>

In [8]:
# Flattening the input
X_train = X_train.reshape(-1, 28 * 28)
X_test = X_test.reshape(-1, 28 * 28)

In [9]:
X_train.shape


Out[9]:
(60000, 784)

In [10]:
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255.0
X_test /= 255.0

In [11]:
X_train[0]


Out[11]:
array([ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.01176471,  0.07058824,  0.07058824,
        0.07058824,  0.49411765,  0.53333336,  0.68627453,  0.10196079,
        0.65098041,  1.        ,  0.96862745,  0.49803922,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.11764706,  0.14117648,  0.36862746,  0.60392159,
        0.66666669,  0.99215686,  0.99215686,  0.99215686,  0.99215686,
        0.99215686,  0.88235295,  0.67450982,  0.99215686,  0.94901961,
        0.7647059 ,  0.25098041,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.19215687,  0.93333334,
        0.99215686,  0.99215686,  0.99215686,  0.99215686,  0.99215686,
        0.99215686,  0.99215686,  0.99215686,  0.98431373,  0.36470589,
        0.32156864,  0.32156864,  0.21960784,  0.15294118,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.07058824,  0.85882354,  0.99215686,  0.99215686,
        0.99215686,  0.99215686,  0.99215686,  0.7764706 ,  0.71372551,
        0.96862745,  0.94509804,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.3137255 ,  0.61176473,  0.41960785,  0.99215686,  0.99215686,
        0.80392158,  0.04313726,  0.        ,  0.16862746,  0.60392159,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.05490196,
        0.00392157,  0.60392159,  0.99215686,  0.35294119,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.54509807,
        0.99215686,  0.74509805,  0.00784314,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.04313726,  0.74509805,  0.99215686,
        0.27450982,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.13725491,  0.94509804,  0.88235295,  0.627451  ,
        0.42352942,  0.00392157,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.31764707,  0.94117647,  0.99215686,  0.99215686,  0.46666667,
        0.09803922,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.17647059,
        0.72941178,  0.99215686,  0.99215686,  0.58823532,  0.10588235,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.0627451 ,  0.36470589,
        0.98823529,  0.99215686,  0.73333335,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.97647059,  0.99215686,
        0.97647059,  0.25098041,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.18039216,  0.50980395,
        0.71764708,  0.99215686,  0.99215686,  0.81176472,  0.00784314,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.15294118,
        0.58039218,  0.89803922,  0.99215686,  0.99215686,  0.99215686,
        0.98039216,  0.71372551,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.09411765,  0.44705883,  0.86666667,  0.99215686,  0.99215686,
        0.99215686,  0.99215686,  0.78823531,  0.30588236,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.09019608,  0.25882354,  0.83529413,  0.99215686,
        0.99215686,  0.99215686,  0.99215686,  0.7764706 ,  0.31764707,
        0.00784314,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.07058824,  0.67058825,  0.85882354,
        0.99215686,  0.99215686,  0.99215686,  0.99215686,  0.7647059 ,
        0.3137255 ,  0.03529412,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.21568628,  0.67450982,
        0.88627452,  0.99215686,  0.99215686,  0.99215686,  0.99215686,
        0.95686275,  0.52156866,  0.04313726,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.53333336,  0.99215686,  0.99215686,  0.99215686,
        0.83137256,  0.52941179,  0.51764709,  0.0627451 ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ], dtype=float32)

In [12]:
from keras.utils.np_utils import to_categorical

In [13]:
y_train_cat = to_categorical(y_train)
y_test_cat = to_categorical(y_test)

In [14]:
y_train[0]


Out[14]:
5

In [15]:
# One-hot encoded labels
y_train_cat[0]


Out[15]:
array([ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.])

In [16]:
y_train_cat.shape


Out[16]:
(60000, 10)

In [17]:
y_test_cat.shape


Out[17]:
(10000, 10)

Fully connected on images


In [18]:
from keras.models import Sequential
from keras.layers import Dense
import keras.backend as K

K.clear_session()

model = Sequential()
model.add(Dense(10, 
                input_dim = 28 * 28, 
                activation = 'relu'))

model.add(Dense(256, 
                activation = 'relu'))
'''
model.add(Dense(128, 
                activation = 'relu'))
'''
model.add(Dense(16, 
                activation = 'relu'))
model.add(Dense(10, 
                activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy',
              optimizer = 'rmsprop',
              metrics = ['accuracy'])

In [19]:
h = model.fit(X_train, 
              y_train_cat, 
              batch_size = 12, 
              epochs = 10, 
              verbose = 2, 
              validation_split = 0.3)


Train on 42000 samples, validate on 18000 samples
Epoch 1/10
7s - loss: 0.4618 - acc: 0.8626 - val_loss: 0.3001 - val_acc: 0.9127
Epoch 2/10
7s - loss: 0.2593 - acc: 0.9253 - val_loss: 0.2568 - val_acc: 0.9278
Epoch 3/10
7s - loss: 0.2254 - acc: 0.9365 - val_loss: 0.2218 - val_acc: 0.9397
Epoch 4/10
7s - loss: 0.2167 - acc: 0.9410 - val_loss: 0.2355 - val_acc: 0.9399
Epoch 5/10
6s - loss: 0.2151 - acc: 0.9430 - val_loss: 0.2449 - val_acc: 0.9390
Epoch 6/10
6s - loss: 0.2169 - acc: 0.9451 - val_loss: 0.2376 - val_acc: 0.9397
Epoch 7/10
7s - loss: 0.2200 - acc: 0.9455 - val_loss: 0.2486 - val_acc: 0.9388
Epoch 8/10
7s - loss: 0.2283 - acc: 0.9454 - val_loss: 0.2472 - val_acc: 0.9437
Epoch 9/10
6s - loss: 0.2301 - acc: 0.9458 - val_loss: 0.2697 - val_acc: 0.9382
Epoch 10/10
7s - loss: 0.2408 - acc: 0.9447 - val_loss: 0.3110 - val_acc: 0.9342

In [20]:
plt.figure(figsize = (12,7))
plt.plot(h.history['acc'])
plt.plot(h.history['val_acc'])
plt.legend(['Training', 'Validation'], loc = 'lower right')
plt.title('Accuracy')
plt.xlabel('Epochs')


Out[20]:
<matplotlib.text.Text at 0x7ff489cb37b8>

In [21]:
test_accuracy = model.evaluate(X_test, y_test_cat)[1]
# Printing the accuracy
test_accuracy * 100


 8128/10000 [=======================>......] - ETA: 0s
Out[21]:
93.359999999999999

Tensor Math


In [22]:
A = np.random.randint(10, size = (2, 3, 4, 5))
B = np.random.randint(10, size = (2, 3))

In [23]:
A


Out[23]:
array([[[[8, 7, 5, 2, 3],
         [5, 0, 1, 8, 9],
         [2, 6, 0, 0, 3],
         [6, 1, 7, 6, 8]],

        [[9, 1, 3, 4, 4],
         [0, 8, 1, 9, 5],
         [9, 4, 8, 8, 9],
         [4, 4, 4, 3, 6]],

        [[0, 6, 2, 4, 4],
         [2, 2, 8, 9, 0],
         [2, 9, 7, 2, 3],
         [9, 8, 5, 4, 5]]],


       [[[2, 9, 7, 4, 9],
         [3, 7, 8, 3, 9],
         [8, 9, 3, 3, 7],
         [5, 7, 4, 2, 0]],

        [[6, 2, 3, 7, 6],
         [4, 6, 0, 3, 3],
         [3, 2, 5, 0, 7],
         [7, 3, 1, 4, 2]],

        [[1, 5, 0, 8, 7],
         [6, 2, 1, 9, 8],
         [6, 4, 2, 8, 4],
         [0, 7, 1, 2, 3]]]])

In [24]:
A[0, 1, 0, 3]


Out[24]:
4

In [25]:
B


Out[25]:
array([[4, 4, 1],
       [2, 0, 4]])

A random colored image


In [26]:
img = np.random.randint(255, 
                        size = (4, 4, 3),
                        dtype = 'uint8')
img


Out[26]:
array([[[195,  52,  82],
        [ 14, 163,  16],
        [157, 123, 223],
        [ 15,  83,  17]],

       [[209, 187, 162],
        [216, 251,  87],
        [173, 185,  75],
        [203,  58,  25]],

       [[209, 174,  91],
        [159, 132,  36],
        [118, 170, 102],
        [ 22,  99, 221]],

       [[147, 207, 122],
        [101,   4, 174],
        [ 67, 206, 156],
        [239, 103, 116]]], dtype=uint8)

In [27]:
plt.figure(figsize = (10, 7))
plt.subplot(221)
plt.imshow(img)
plt.title("All Channels combined")

plt.subplot(222)
plt.imshow(img[:, : , 0], 
           cmap = 'Reds')
plt.title("Red channel")

plt.subplot(223)
plt.imshow(img[:, : , 1], 
           cmap = 'Greens')
plt.title("Green channel")

plt.subplot(224)
plt.imshow(img[:, : , 2], 
           cmap = 'Blues')
plt.title("Blue channel")


Out[27]:
<matplotlib.text.Text at 0x7ff489aebf60>

Tensor operations


In [28]:
2 * A


Out[28]:
array([[[[16, 14, 10,  4,  6],
         [10,  0,  2, 16, 18],
         [ 4, 12,  0,  0,  6],
         [12,  2, 14, 12, 16]],

        [[18,  2,  6,  8,  8],
         [ 0, 16,  2, 18, 10],
         [18,  8, 16, 16, 18],
         [ 8,  8,  8,  6, 12]],

        [[ 0, 12,  4,  8,  8],
         [ 4,  4, 16, 18,  0],
         [ 4, 18, 14,  4,  6],
         [18, 16, 10,  8, 10]]],


       [[[ 4, 18, 14,  8, 18],
         [ 6, 14, 16,  6, 18],
         [16, 18,  6,  6, 14],
         [10, 14,  8,  4,  0]],

        [[12,  4,  6, 14, 12],
         [ 8, 12,  0,  6,  6],
         [ 6,  4, 10,  0, 14],
         [14,  6,  2,  8,  4]],

        [[ 2, 10,  0, 16, 14],
         [12,  4,  2, 18, 16],
         [12,  8,  4, 16,  8],
         [ 0, 14,  2,  4,  6]]]])

In [29]:
A + A


Out[29]:
array([[[[16, 14, 10,  4,  6],
         [10,  0,  2, 16, 18],
         [ 4, 12,  0,  0,  6],
         [12,  2, 14, 12, 16]],

        [[18,  2,  6,  8,  8],
         [ 0, 16,  2, 18, 10],
         [18,  8, 16, 16, 18],
         [ 8,  8,  8,  6, 12]],

        [[ 0, 12,  4,  8,  8],
         [ 4,  4, 16, 18,  0],
         [ 4, 18, 14,  4,  6],
         [18, 16, 10,  8, 10]]],


       [[[ 4, 18, 14,  8, 18],
         [ 6, 14, 16,  6, 18],
         [16, 18,  6,  6, 14],
         [10, 14,  8,  4,  0]],

        [[12,  4,  6, 14, 12],
         [ 8, 12,  0,  6,  6],
         [ 6,  4, 10,  0, 14],
         [14,  6,  2,  8,  4]],

        [[ 2, 10,  0, 16, 14],
         [12,  4,  2, 18, 16],
         [12,  8,  4, 16,  8],
         [ 0, 14,  2,  4,  6]]]])

In [30]:
A.shape


Out[30]:
(2, 3, 4, 5)

In [31]:
B.shape


Out[31]:
(2, 3)

In [32]:
np.tensordot(A, 
             B, 
             axes = ([0, 1], [0, 1]))


Out[32]:
array([[ 76,  76,  48,  68,  78],
       [ 52,  56,  36, 119, 106],
       [ 86,  83,  53,  72,  81],
       [ 59,  70,  61,  52,  73]])

In [33]:
np.tensordot(A, 
             B, 
             axes = ([0], [0])).shape


Out[33]:
(3, 4, 5, 3)

1D convolution


In [34]:
a = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], 
             dtype='float32')

In [35]:
b = np.array([-1, 1], 
             dtype='float32')

In [36]:
c = np.convolve(a, b)

In [37]:
a


Out[37]:
array([ 0.,  0.,  0.,  0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,
        0.,  0.], dtype=float32)

In [38]:
b


Out[38]:
array([-1.,  1.], dtype=float32)

In [39]:
c


Out[39]:
array([ 0.,  0.,  0.,  0.,  0., -1.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,
        0.,  0.,  0.], dtype=float32)

In [40]:
plt.subplot(211)
plt.plot(a, 'o-')

plt.subplot(212)
plt.plot(c, 'o-')


Out[40]:
[<matplotlib.lines.Line2D at 0x7ff4897a4f98>]

Image filters with convolutions


In [41]:
from scipy.ndimage.filters import convolve
from scipy.signal import convolve2d
from scipy import misc

In [42]:
img = misc.ascent()

In [43]:
img.shape


Out[43]:
(512, 512)

In [44]:
plt.imshow(img, 
           cmap = 'gray')


Out[44]:
<matplotlib.image.AxesImage at 0x7ff470df0198>

In [45]:
h_kernel = np.array([[ 1,  2,  1],
                     [ 0,  0,  0],
                     [-1, -2, -1]])

In [46]:
plt.imshow(h_kernel, 
           cmap = 'gray')


Out[46]:
<matplotlib.image.AxesImage at 0x7ff470d554a8>

In [47]:
res = convolve2d(img, h_kernel)

plt.imshow(res, 
           cmap = 'gray')


Out[47]:
<matplotlib.image.AxesImage at 0x7ff470d30ef0>

Convolutional neural networks


In [48]:
from keras.layers import Conv2D

In [49]:
img.shape


Out[49]:
(512, 512)

In [50]:
plt.figure(figsize = (7, 7))
plt.imshow(img, 
           cmap = 'gray')


Out[50]:
<matplotlib.image.AxesImage at 0x7ff470c9d550>

In [51]:
img_tensor = img.reshape((1, 512, 512, 1))

In [52]:
model = Sequential()
model.add(Conv2D(filters = 1, 
                 kernel_size = (3, 3), 
                 strides = (2,1), 
                 input_shape = (512, 512, 1)))
model.compile('adam', 'mse')

In [53]:
img_pred_tensor = model.predict(img_tensor)

In [54]:
img_pred_tensor.shape


Out[54]:
(1, 255, 510, 1)

In [55]:
img_pred = img_pred_tensor[0, :, :, 0]

In [56]:
plt.imshow(img_pred, 
           cmap = 'gray')


Out[56]:
<matplotlib.image.AxesImage at 0x7ff4709c9b00>

In [57]:
weights = model.get_weights()

In [58]:
weights[0].shape


Out[58]:
(3, 3, 1, 1)

In [59]:
plt.imshow(weights[0][:, :, 0, 0], 
           cmap = 'gray')


Out[59]:
<matplotlib.image.AxesImage at 0x7ff4709b67b8>

In [60]:
weights[0] = np.ones(weights[0].shape)

In [61]:
model.set_weights(weights)

In [62]:
img_pred_tensor = model.predict(img_tensor)

In [63]:
img_pred = img_pred_tensor[0, :, :, 0]

In [64]:
plt.imshow(img_pred, 
           cmap = 'gray')


Out[64]:
<matplotlib.image.AxesImage at 0x7ff47091ae10>

In [65]:
model = Sequential()
model.add(Conv2D(filters = 1, 
                 kernel_size = (3, 3), 
                 input_shape = (512, 512, 1), 
                 padding='same'))
model.compile('adam', 'mse')

img_pred_tensor = model.predict(img_tensor)


img_pred_tensor.shape


Out[65]:
(1, 512, 512, 1)

In [66]:
img_pred_tensor = img_pred_tensor[0, :, :, 0]

plt.imshow(img_pred_tensor, 
           cmap = 'gray')


Out[66]:
<matplotlib.image.AxesImage at 0x7ff4708540b8>

Pooling layers


In [67]:
from keras.layers import MaxPool2D, AvgPool2D

In [68]:
model = Sequential()
model.add(MaxPool2D(pool_size = (5, 5), 
                    input_shape = (512, 512, 1)))
model.compile('adam', 'mse')

In [69]:
img_pred = model.predict(img_tensor)[0, :, :, 0]

In [70]:
plt.imshow(img_pred, 
           cmap = 'gray')


Out[70]:
<matplotlib.image.AxesImage at 0x7ff4707e04a8>

In [71]:
model = Sequential()
model.add(AvgPool2D(pool_size = (5, 5), 
                    input_shape = (512, 512, 1)))
model.compile('adam', 'mse')

In [72]:
img_pred = model.predict(img_tensor)[0, :, :, 0]
plt.imshow(img_pred, 
           cmap = 'gray')


Out[72]:
<matplotlib.image.AxesImage at 0x7ff47077ab38>

Final architecture


In [73]:
X_train = X_train.reshape(-1, 28, 28, 1)
X_test = X_test.reshape(-1, 28, 28, 1)

In [74]:
X_train.shape


Out[74]:
(60000, 28, 28, 1)

In [75]:
from keras.layers import Flatten, Activation

In [76]:
K.clear_session()

model = Sequential()

model.add(Conv2D(8, 
                 (3, 3), 
                 input_shape = (28, 28, 1)))
model.add(MaxPool2D(pool_size = (2, 2)))
model.add(Activation('relu'))

model.add(Flatten())

model.add(Dense(32, 
                activation = 'relu'))

model.add(Dense(10, 
                activation = 'softmax'))

model.compile(loss = 'categorical_crossentropy',
              optimizer = 'rmsprop',
              metrics = ['accuracy'])

In [77]:
model.summary()


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 26, 26, 8)         80        
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 13, 13, 8)         0         
_________________________________________________________________
activation_1 (Activation)    (None, 13, 13, 8)         0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 1352)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 32)                43296     
_________________________________________________________________
dense_2 (Dense)              (None, 10)                330       
=================================================================
Total params: 43,706
Trainable params: 43,706
Non-trainable params: 0
_________________________________________________________________

In [78]:
model.fit(X_train, 
          y_train_cat, 
          batch_size = 12,
          epochs = 2, 
          verbose = 2, 
          validation_split = 0.3)


Train on 42000 samples, validate on 18000 samples
Epoch 1/2
23s - loss: 0.2937 - acc: 0.9131 - val_loss: 0.1650 - val_acc: 0.9515
Epoch 2/2
22s - loss: 0.1292 - acc: 0.9619 - val_loss: 0.1105 - val_acc: 0.9668
Out[78]:
<keras.callbacks.History at 0x7ff470550748>

In [79]:
evaluated = model.evaluate(X_test, y_test_cat)


 9792/10000 [============================>.] - ETA: 0s

In [80]:
evaluated[1] * 100


Out[80]:
97.049999999999997

Exercise 1

You've been hired by a shipping company to overhaul the way they route mail, parcels and packages. They want to build an image recognition system capable of recognizing the digits in the zipcode on a package, so that it can be automatically routed to the correct location. You are tasked to build the digit recognition system. Luckily, you can rely on the MNIST dataset for the intial training of your model!

Build a deep convolutional neural network with at least two convolutional and two pooling layers before the fully connected layer.

  • Start from the network we have just built
  • Insert a Conv2D layer after the first MaxPool2D, give it 64 filters.
  • Insert a MaxPool2D after that one
  • Insert an Activation layer
  • retrain the model
  • does performance improve?
  • how many parameters does this new model have? More or less than the previous model? Why?
  • how long did this second model take to train? Longer or shorter than the previous model? Why?
  • did it perform better or worse than the previous model?

In [81]:
X_train.shape, X_test.shape, y_train_cat.shape, y_test_cat.shape


Out[81]:
((60000, 28, 28, 1), (10000, 28, 28, 1), (60000, 10), (10000, 10))

In [82]:
from keras.layers import Input, Dense, Conv2D, MaxPool2D, Activation, Flatten
import keras.backend as K
from keras.models import Model
from keras.optimizers import Adam

In [86]:
K.clear_session()
inp = Input(shape = (28, 28, 1 ))
net = Conv2D(filters = 64, 
             kernel_size = (2, 2), 
             activation = 'relu', 
             padding = 'valid')(inp)
net = MaxPool2D(pool_size = (2, 2), 
                strides = (2, 2), 
                padding = 'valid')(net)
net = Conv2D(filters = 8, 
             kernel_size = (2, 2), 
             activation = 'relu', 
             padding = 'valid')(net)
net = MaxPool2D(pool_size = (2, 2), 
                strides = (2, 2), 
                padding = 'valid')(net)
net = Activation(activation = 'relu')(net)
net = Flatten()(net)
prediction = Dense(10, activation = 'softmax')(net)

In [87]:
model = Model(inputs = inp, outputs = prediction)
model.compile(optimizer = Adam(), 
              loss = 'categorical_crossentropy', 
              metrics = ['accuracy'])

In [88]:
model.summary()


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 28, 28, 1)         0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 27, 27, 64)        320       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 13, 13, 64)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 12, 12, 8)         2056      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 6, 6, 8)           0         
_________________________________________________________________
activation_1 (Activation)    (None, 6, 6, 8)           0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 288)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 10)                2890      
=================================================================
Total params: 5,266
Trainable params: 5,266
Non-trainable params: 0
_________________________________________________________________

In [89]:
model.fit(X_train, 
          y_train_cat, 
          batch_size = 50, 
          validation_split = 0.2,
          epochs = 5,
          verbose = 2)


Train on 48000 samples, validate on 12000 samples
Epoch 1/5
44s - loss: 0.4742 - acc: 0.8544 - val_loss: 0.1970 - val_acc: 0.9437
Epoch 2/5
43s - loss: 0.1652 - acc: 0.9497 - val_loss: 0.1205 - val_acc: 0.9657
Epoch 3/5
43s - loss: 0.1150 - acc: 0.9651 - val_loss: 0.0939 - val_acc: 0.9738
Epoch 4/5
44s - loss: 0.0941 - acc: 0.9708 - val_loss: 0.0868 - val_acc: 0.9753
Epoch 5/5
43s - loss: 0.0803 - acc: 0.9748 - val_loss: 0.0906 - val_acc: 0.9738
Out[89]:
<keras.callbacks.History at 0x7ff470188c88>

Exercise 2

Pleased with your performance with the digits recognition task, your boss decides to challenge you with a harder task. Their online branch allows people to upload images to a website that generates and prints a postcard that is shipped to destination. Your boss would like to know what images people are loading on the site in order to provide targeted advertising on the same page, so he asks you to build an image recognition system capable of recognizing a few objects. Luckily for you, there's a dataset ready made with a collection of labeled images. This is the Cifar 10 Dataset, a very famous dataset that contains images for 10 different categories:

  • airplane
  • automobile
  • bird
  • cat
  • deer
  • dog
  • frog
  • horse
  • ship
  • truck

In this exercise we will reach the limit of what you can achieve on your laptop and get ready for the next session on cloud GPUs.

Here's what you have to do:

  • load the cifar10 dataset using keras.datasets.cifar10.load_data()
  • display a few images, see how hard/easy it is for you to recognize an object with such low resolution
  • check the shape of X_train, does it need reshape?
  • check the scale of X_train, does it need rescaling?
  • check the shape of y_train, does it need reshape?
  • build a model with the following architecture, and choose the parameters and activation functions for each of the layers:
    • conv2d
    • conv2d
    • maxpool
    • conv2d
    • conv2d
    • maxpool
    • flatten
    • dense
    • output
  • compile the model and check the number of parameters
  • attempt to train the model with the optimizer of your choice. How fast does training proceed?
  • If training is too slow (as expected) stop the execution and move to the next session!

In [138]:
from keras.datasets import cifar10

In [139]:
(X_train, y_train), (X_test, y_test) = cifar10.load_data()

In [140]:
X_train.shape


Out[140]:
(50000, 32, 32, 3)

In [141]:
X_train[0, :, :, 0]


Out[141]:
array([[ 59,  43,  50, ..., 158, 152, 148],
       [ 16,   0,  18, ..., 123, 119, 122],
       [ 25,  16,  49, ..., 118, 120, 109],
       ..., 
       [208, 201, 198, ..., 160,  56,  53],
       [180, 173, 186, ..., 184,  97,  83],
       [177, 168, 179, ..., 216, 151, 123]], dtype=uint8)

In [142]:
X_train[0, :, :, 1]


Out[142]:
array([[ 62,  46,  48, ..., 132, 125, 124],
       [ 20,   0,   8, ...,  88,  83,  87],
       [ 24,   7,  27, ...,  84,  84,  73],
       ..., 
       [170, 153, 161, ..., 133,  31,  34],
       [139, 123, 144, ..., 148,  62,  53],
       [144, 129, 142, ..., 184, 118,  92]], dtype=uint8)

In [143]:
X_train[0, :, :, 2]


Out[143]:
array([[ 63,  45,  43, ..., 108, 102, 103],
       [ 20,   0,   0, ...,  55,  50,  57],
       [ 21,   0,   8, ...,  50,  50,  42],
       ..., 
       [ 96,  34,  26, ...,  70,   7,  20],
       [ 96,  42,  30, ...,  94,  34,  34],
       [116,  94,  87, ..., 140,  84,  72]], dtype=uint8)

In [144]:
np.max(X_train[0, :, :, :])


Out[144]:
255

In [145]:
np.min(X_train[0, :, :, :])


Out[145]:
0

In [146]:
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

X_train = X_train / 255.0
X_test = X_test / 255.0

In [147]:
from keras.utils import to_categorical
y_train_cat = to_categorical(y_train)
y_test_cat = to_categorical(y_test)

In [148]:
y_train_cat[:5]


Out[148]:
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

In [149]:
print(y_train_cat.shape)


(50000, 10)

In [150]:
y_test_cat[:5]


Out[150]:
array([[ 0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.]])

In [151]:
print(y_test_cat.shape)


(10000, 10)

In [172]:
'''conv2d
conv2d
maxpool

conv2d
conv2d
maxpool
flatten
dense
output
'''

from keras.models import Model
from keras.layers import Input, Dense, Conv2D, MaxPool2D, Flatten
from keras.regularizers import l2

# Inputs 32 x 32 x 3
inp = Input(shape = (32, 32, 3))

net = Conv2D(filters = 8, 
             kernel_size = (2, 2), 
             padding = 'same')(inp)
net = Conv2D(filters = 8, 
             kernel_size = (2, 2), 
             padding = 'same')(net)
net = MaxPool2D(pool_size = (2, 2), padding = 'valid')(net)


net = Conv2D(filters = 8, 
             kernel_size = (2, 2), 
             padding = 'same')(net)
net = Conv2D(filters = 8, 
             kernel_size = (2, 2), 
             padding = 'same')(net)
net = MaxPool2D(pool_size = (2, 2), padding = 'valid')(net)


net = Flatten()(net)
net = Dense(units = 10, 
            activation = 'relu')(net)
prediction = Dense(units = 10, 
                   activation = 'softmax')(net)

In [181]:
model = Model(inputs = [inp], outputs = [prediction])

In [182]:
from keras.optimizers import Adam

In [188]:
model.compile(optimizer = Adam(), 
              loss = 'categorical_crossentropy', 
              metrics = ['accuracy'])

In [189]:
model.summary()


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_11 (InputLayer)        (None, 32, 32, 3)         0         
_________________________________________________________________
conv2d_20 (Conv2D)           (None, 32, 32, 8)         104       
_________________________________________________________________
conv2d_21 (Conv2D)           (None, 32, 32, 8)         264       
_________________________________________________________________
max_pooling2d_9 (MaxPooling2 (None, 16, 16, 8)         0         
_________________________________________________________________
conv2d_22 (Conv2D)           (None, 16, 16, 8)         264       
_________________________________________________________________
conv2d_23 (Conv2D)           (None, 16, 16, 8)         264       
_________________________________________________________________
max_pooling2d_10 (MaxPooling (None, 8, 8, 8)           0         
_________________________________________________________________
flatten_4 (Flatten)          (None, 512)               0         
_________________________________________________________________
dense_4 (Dense)              (None, 10)                5130      
_________________________________________________________________
dense_5 (Dense)              (None, 10)                110       
=================================================================
Total params: 6,136
Trainable params: 6,136
Non-trainable params: 0
_________________________________________________________________

In [190]:
model.fit(X_train, 
          y_train_cat, 
          batch_size = 50, 
          validation_split = 0.2,
          epochs = 5,
          verbose = 2)


Train on 40000 samples, validate on 10000 samples
Epoch 1/5
27s - loss: 1.3703 - acc: 0.5163 - val_loss: 1.3708 - val_acc: 0.5155
Epoch 2/5
27s - loss: 1.3159 - acc: 0.5365 - val_loss: 1.3660 - val_acc: 0.5227
Epoch 3/5
27s - loss: 1.2790 - acc: 0.5501 - val_loss: 1.2944 - val_acc: 0.5493
Epoch 4/5
27s - loss: 1.2474 - acc: 0.5617 - val_loss: 1.3014 - val_acc: 0.5512
Epoch 5/5
27s - loss: 1.2212 - acc: 0.5703 - val_loss: 1.2585 - val_acc: 0.5572
Out[190]:
<keras.callbacks.History at 0x7ff47a0cbe48>